package com.hsmw.api.config.jwt;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hsmw.api.service.*;
import com.htn.common.core.constant.AccountTypeEnum;
import com.htn.common.core.constant.CommonConstants;
import com.htn.common.core.constant.PicTypeEnum;
import com.hsmw.common.context.SecurityConstants;
import com.htn.common.core.utils.StringUtils;
import com.hsmw.domain.*;
import com.htn.common.core.utils.SecurityUtils;
import com.tencent.xinge.bean.AccountType;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Stream;

/**
 * @author shuang.kou
 * @description 获取当前请求的用户
 */
@Component
public class CurrentHsmwUserUtils {

	@Autowired
	private HsmwPmUserService hsmwPmUserService;

	@Autowired
	private HsmwConcreteUserService hsmwConcreteUserService;

	@Autowired
	private HsmwDriverUserService hsmwDriverUserService;

	@Autowired
	private HsmwBizUserService hsmwBizUserService;

	@Autowired
	private HsmwHtnUserService hsmwHtnUserService;

	@Autowired
	private HsmwTpspUserService hsmwTpspUserService;

	@Autowired
	private HsmwSaleUserService hsmwSaleUserService;

	@Autowired
	private HsmwPictureService hsmwPictureService;

	private static List<HsmwPicture> headPicHub =new ArrayList();

	public LoginUser getCurrentUserInfo() {
		LoginUser loginUser =new LoginUser();
		JSONObject dto = SecurityUtils.getUserInfo();
		loginUser.setUsername(dto.getString("username"));
		loginUser.setName(dto.getString("name"));
		loginUser.setId(dto.getString("userId"));
		loginUser.setType(dto.getString("type"));
		return loginUser;
	}

	public HsmwBizUser getCurrentBizUserInfo() {
		HsmwBizUser user = new HsmwBizUser();
		LoginUser loginUser =  getCurrentUserInfo();
		if(AccountTypeEnum.BIZ.getValue().equals(loginUser.getType())){
			user = hsmwBizUserService.getById(loginUser.getId());
		}
		return user;
	}
	public HsmwDriverUser getCurrentDriverUserInfo() {
		HsmwDriverUser user = new HsmwDriverUser();
		LoginUser loginUser =  getCurrentUserInfo();
		if(AccountTypeEnum.DRIVER.getValue().equals(loginUser.getType())){
			user = hsmwDriverUserService.getById(loginUser.getId());
		}
		return user;
	}

	public HsmwTpspUser getCurrentTpspUserInfo() {
		HsmwTpspUser user = new HsmwTpspUser();
		LoginUser loginUser =  getCurrentUserInfo();
		if(AccountTypeEnum.TPSP.getValue().equals(loginUser.getType())){
			user = hsmwTpspUserService.getById(loginUser.getId());
		}
		return user;
	}

	public HsmwConcreteUser getCurrentConcreteUserInfo() {
		HsmwConcreteUser user = new HsmwConcreteUser();
		LoginUser loginUser =  getCurrentUserInfo();
		if(AccountTypeEnum.CONCRETE.getValue().equals(loginUser.getType())){
			user = hsmwConcreteUserService.getById(loginUser.getId());
		}
		return user;
	}
	//
	public HsmwHtnUser getCurrentHtnUserInfo() {
		HsmwHtnUser user = new HsmwHtnUser();
		LoginUser loginUser =  getCurrentUserInfo();
		if(AccountTypeEnum.HTN.getValue().equals(loginUser.getType())){
			user = hsmwHtnUserService.getById(loginUser.getId());
		}
		return user;
	}

	public HsmwPmUser getCurrentPmUserInfo() {
		HsmwPmUser user = new HsmwPmUser();
		LoginUser loginUser =  getCurrentUserInfo();
		if(AccountTypeEnum.PM.getValue().equals(loginUser.getType())){
			user = hsmwPmUserService.getById(loginUser.getId());
		}
		return user;
	}


	public HsmwSaleUser getCurrentSaleUserInfo() {
		HsmwSaleUser user = new HsmwSaleUser();
		LoginUser loginUser =  getCurrentUserInfo();
		if(AccountTypeEnum.SALE.getValue().equals(loginUser.getType())){
			user = hsmwSaleUserService.getById(loginUser.getId());
		}
		return user;
	}

	private String getCurrentHsmwUserAuthorities() {

		return null;
	}

	public    String   getRandomHeadSculpture(){
		if(CollectionUtil.isNotEmpty(headPicHub)){
			// 创建一个生产随机数的对象
			int index = new Random().nextInt(10);
			HsmwPicture pictures = headPicHub.get(index);
			return pictures.getPicRealPath();
		}else {
			List<HsmwPicture> pics = hsmwPictureService.list(new LambdaQueryWrapper<HsmwPicture>()
					.eq(HsmwPicture::getPicType, PicTypeEnum.USER_REG_PIC.getValue()));
			headPicHub.addAll(pics);
			// 创建一个生产随机数的对象
			int index = new Random().nextInt(10);
			HsmwPicture pictures = headPicHub.get(index);
			return pictures.getPicRealPath();
		}
	}



}
